package com.wanxi.controller;

import com.wanxi.model.ProductModel;
import com.wanxi.model.ResultModel;
import com.wanxi.service.ProductService;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.mapping.IndexCoordinates;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author 杨金乾
 */
@Controller
@CrossOrigin(origins = "*")
public class ProductController{
    @Autowired
    ProductService productService;

    @Autowired
    SendMessageController sendMessageController;
    @Resource
    ElasticsearchRestTemplate elasticsearchRestTemplate;
    private String table="产品";
    @RequestMapping("/back/product/add")
    @ResponseBody
    @PreAuthorize("hasAuthority('subPermission') or hasAuthority('allPermission')")
    protected ResultModel productAdd(ProductModel model) {
        ResultModel resultModel = productService.add(model);
        sendMessageController.addUserMessages(table);
//        6.返回前端
        return resultModel;
    }

    @RequestMapping("/back/product/delete")
    @ResponseBody
    @PreAuthorize("hasAuthority('subPermission') or hasAuthority('allPermission')")
    protected ResultModel productDelete(ProductModel model) {
        ResultModel resultModel = productService.delete(model);
        sendMessageController.deleteUserMessages(table);
        String id= String.valueOf(model.getId());
        elasticsearchRestTemplate.delete(id, IndexCoordinates.of("product"));
//        6.返回前端
        return resultModel;
    }

    @RequestMapping("/back/product/findAll")
    @ResponseBody
    @PreAuthorize("hasAuthority('subPermission') or hasAuthority('allPermission')or hasAuthority('unaccessible')")
    protected ResultModel productFindAll(ProductModel model) {
        ResultModel resultModel = productService.findAll(model);
        sendMessageController.findAllUserMessages(table);
//        6.返回前端
        return resultModel;
    }

    @RequestMapping("/back/product/findById")
    @ResponseBody
    @PreAuthorize("hasAuthority('subPermission') or hasAuthority('allPermission')or hasAuthority('unaccessible')")
    protected ResultModel productFindById(ProductModel model) {
        ResultModel resultModel = productService.findById(model);
        sendMessageController.findByIdUserMessages(table);
//        6.返回前端
        return resultModel;
    }

    @RequestMapping("/back/product/enable")
    @ResponseBody
    @PreAuthorize("hasAuthority('subPermission') or hasAuthority('allPermission')or hasAuthority('unaccessible')")
    protected ResultModel productUpdateEnable(ProductModel model) {
        ResultModel resultModel = productService.enable(model);
        sendMessageController.updateEnableMessages(table);
//        6.返回前端
        return resultModel;
    }

    @RequestMapping("/back/product/isRecommend")
    @ResponseBody
    @PreAuthorize("hasAuthority('subPermission') or hasAuthority('allPermission')or hasAuthority('unaccessible')")
    protected ResultModel productUpdateIsRecommend(ProductModel model) {
        ResultModel resultModel = productService.isRecommend(model);
        sendMessageController.updateIsRecommendMessages(table);
//        6.返回前端
        return resultModel;
    }

    @RequestMapping("/back/product/isShow")
    @ResponseBody
    @PreAuthorize("hasAuthority('subPermission') or hasAuthority('allPermission')or hasAuthority('unaccessible')")
    protected ResultModel productUpdateIsShow(ProductModel model) {
        ResultModel resultModel = productService.isShow(model);
        sendMessageController.updateIsShowMessages(table);
//        6.返回前端
        return resultModel;
    }

    @RequestMapping("/back/product/update")
    @ResponseBody
    @PreAuthorize("hasAuthority('subPermission') or hasAuthority('allPermission')")
    protected ResultModel productUpdate(ProductModel model) {
        ResultModel resultModel = productService.update(model);
        sendMessageController.updateMessages(table);
//        6.返回前端
        return resultModel;
    }

    @RequestMapping("/back/product/whereShow")
    @ResponseBody
    @PreAuthorize("hasAuthority('subPermission') or hasAuthority('allPermission')or hasAuthority('unaccessible')")
    protected ResultModel productUpdateWhereShow(ProductModel model) {
        ResultModel resultModel = productService.whereShow(model);
        sendMessageController.updateWhereShowMessages(table);
//        6.返回前端
        return resultModel;
    }
    @RequestMapping("/back/product/data")
    @ResponseBody
    @PreAuthorize("hasAuthority('subPermission') or hasAuthority('allPermission')or hasAuthority('unaccessible')")
    public List<ProductModel> findByProductModelEs(ProductModel productModel) {
        sendMessageController.findAllUserMessages(table);
        return productService.searchByProductModelEs(productModel);
    }
    @RequestMapping("/back/product/esData")
    @ResponseBody
    public String importEsByContent(ProductModel productModel) {
       this.productService.saveEsByContent(productModel);
        return "success";
    }

}
